//
//  NewsViewController.m
//  CarWorld
//
//  Created by 曹杨杨 on 15/3/25.
//  Copyright (c) 2015年 CarWorldTeam. All rights reserved.
//

#import "NewsViewController.h"
#import "c_NewsListData.h"
#import "UIImageView+WebCache.h"
#import "NewsImageScrollView.h"
#import "NewsTableCell.h"
#import "NewDetailViewController.h"
@interface NewsViewController ()

@property(nonatomic, retain) NSMutableArray *newsArray;
@property(nonatomic, retain) NewsImageScrollView *newsScrollView;
//@property(nonatomic,  retain)NSMutableArray *btnArray;


@end
@implementation NewsViewController

-(void)dealloc
{
//    [_newsScrollView release];
    [_newsArray release];
    [super dealloc];
}



//首页轮播图
- (void)getNewsScrollUrl
{
    NSURL *url = [NSURL URLWithString:@"http://mobile.auto.sohu.com/mcms/external/getFocusNews.at?newsType=11"];
    NSURLRequest *requst = [NSURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:requst queue:[[[NSOperationQueue alloc] autorelease] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (data != nil) {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
            NSArray *arr = dic[@"RESULT"];
            //NSLog(@"%@ ",arr);
//            NSInteger i=0;
            for (NSDictionary *pdic in arr) {
                //NSLog(@"-------%@ ",pdic);
                c_NewsListData *cellData = [[c_NewsListData alloc] init];
//                [cellData addObserver:self forKeyPath:@"image" options:NSKeyValueObservingOptionNew context:_btnArray[i]];
                [cellData setValuesForKeysWithDictionary:pdic];
                [_newsArray addObject:cellData];
//                 NSLog(@"-----%@",cellData.image);

//                UIButton *btn = (UIButton *)_btnArray[i];
//                [btn setImage:cellData.image forState:UIControlStateNormal];
                
                [cellData release];
//                i++;
               
            }
            //[self.view drawRect:self.view.frame];
            //NSLog(@"-----%@",_newsArray);
            //NSLog(@"%@",_newsArray);
        //数组中存放的是cellData的值.所以取值时要对应取值
           [_newsScrollView.AAA.image setImageWithURL:[NSURL URLWithString:[_newsArray[0] header_img_url]]];
//            [_BBB setImageWithURL:[NSURL URLWithString:[_newsArray[1] header_img_url]]];
//            [_CCC setImageWithURL:[NSURL URLWithString:[_newsArray[2] header_img_url]]];
//            [_DDD setImageWithURL:[NSURL URLWithString:[_newsArray[3] header_img_url]]];
            
            NSLog(@"%@",[_newsArray[0] title]);
//            _label1.text = [NSString stringWithString:[_newsArray[0] title]];
//            _label1.text = @"dajkjkd";
            [self.view drawRect:self.view.frame];
//            NSLog(@"%@",[_newsArray[0] header_img_url]);
//            [_btn1 setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[_newsArray[0] header_img_url]]]] forState:UIControlStateNormal];
//            [_btn1 setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://auto.sohu.com/mapp/upload/image/2015/03/1425430476060.480.240.jpg"]]] forState:UIControlStateNormal];
            
        }
    }];
    
}

//- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
//{
//    NSLog(@"go to ");
//    UIButton *btn = (UIButton *)context;
//    UIImage *image = (UIImage *)[change objectForKey:@"new"];
//    [btn setImage:image forState:UIControlStateNormal];
//

- (void)abc
{
    NSLog(@"执行跳转到详情界面方法");
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.newsArray = [NSMutableArray array];
    [self getNewsScrollUrl];
    
    NSLog(@"第一个");
    self.navigationItem.title = @"新闻时讯";
    /*
    {
//    UIScrollView *newsScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 160)];
//    newsScrollView.contentSize = CGSizeMake(1280, 100);
//    newsScrollView.pagingEnabled = YES;
//    
//    
//    //~~~~~~~~~~~~~~~~轮播图~~~~~~~~~~~~~~~~~~
//   
//    
//    _AAA = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 160)];
//    _AAA.userInteractionEnabled = YES;
//    [newsScrollView addSubview:_AAA];
//    _btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
//    _btn1.frame = CGRectMake(0, 0, 320, 160);
//    [_btn1 addTarget:self action:@selector(abc) forControlEvents:UIControlEventTouchUpInside];
//    [_AAA addSubview:_btn1];
//    
//    
//    _BBB = [[UIImageView alloc] initWithFrame:CGRectMake(320, 0, 320, 160)];
//    _BBB.userInteractionEnabled = YES;
//    [newsScrollView addSubview:_BBB];
//    _btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
//    _btn2.frame = CGRectMake(320, 0, 320 ,160);
////    [_btnArray addObject:btn2];
//    [_btn2 addTarget:self action:@selector(abc) forControlEvents:UIControlEventTouchUpInside];
//    [newsScrollView addSubview:_btn2];
//    
//    _CCC = [[UIImageView alloc] initWithFrame:CGRectMake(640, 0, 320, 160)];
//    _CCC.userInteractionEnabled = YES;
//    [newsScrollView addSubview:_CCC];
//    _btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
//    _btn3.frame = CGRectMake(640, 0, 320 ,160);
//    [_btn3 addTarget:self action:@selector(abc) forControlEvents:UIControlEventTouchUpInside];
//    [newsScrollView addSubview:_btn2];
////    [_btnArray addObject:button3];
//    [newsScrollView addSubview:_btn3];
//    
//    _DDD = [[UIImageView alloc] initWithFrame:CGRectMake(960, 0, 320, 160)];
//    _DDD.userInteractionEnabled = YES;
//    [newsScrollView addSubview:_DDD];
//    _btn4 = [UIButton buttonWithType:UIButtonTypeCustom];
//    _btn4.frame = CGRectMake(960, 0, 320 ,160);
//    [_btn4 addTarget:self action:@selector(abc) forControlEvents:UIControlEventTouchUpInside];
//    [newsScrollView addSubview:_btn2];
////    [_btnArray addObject:button4];
//    [newsScrollView addSubview:_btn4];
//    
//    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//    _label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 130, 320, 30)];
//    _label1.textColor = [UIColor whiteColor];
//    _label1.font = [UIFont systemFontOfSize:14 weight:2];
//    [newsScrollView addSubview:_label1];
    }
    */
    //创建tableView
    UITableView *newstab = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 425) style:UITableViewStylePlain];
    newstab.dataSource = self;
    newstab.delegate = self;
    

    
    CGRect rect = [UIScreen mainScreen].bounds;
    
    NewsImageScrollView *newsScrollView = [[NewsImageScrollView  alloc] initWithFrame:CGRectMake(0, 94, rect.size.width, 160)];
    newsScrollView.contentSize = CGSizeMake(rect.size.width*4, 160);
    newsScrollView.pagingEnabled = YES;
    [newsScrollView.btn1 addTarget:self action:@selector(abc) forControlEvents:UIControlEventTouchUpInside];
    [newsScrollView.btn1 addTarget:self action:@selector(abc) forControlEvents:UIControlEventTouchUpInside];
    [newsScrollView.btn1 addTarget:self action:@selector(abc) forControlEvents:UIControlEventTouchUpInside];
    [newsScrollView.btn1 addTarget:self action:@selector(abc) forControlEvents:UIControlEventTouchUpInside];
    newstab.tableHeaderView = newsScrollView;
    [self.view addSubview:newstab];

    [newstab release];
    [newsScrollView release];
}


//- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
//{
//    return 100;
//}

//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//    return 160;
//}

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return 50;
//}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"new";
    NewsTableCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[[NewsTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
    }
  
    
    cell.textLabel.text = @"实验";
    cell.tintColor = [UIColor redColor];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    MovieDetailViewController *movieDetail = [[MovieDetailViewController alloc] init];
//    movieDetail.movieId = [_array[indexPath.section] movieId];
//    [self.navigationController pushViewController:movieDetail animated:YES];
//    [movieDetail release];
    
    NewDetailViewController *newsDetail = [[NewDetailViewController alloc] init];
    [self.navigationController pushViewController:newsDetail animated:YES];
    [newsDetail release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
